package com.jinhua.medical.manager.common;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by kriss on 2017/5/19.
 */
public class BaseController {

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    protected HttpHeaders headers;
    protected Integer sort;
    protected Integer offset;
    protected Integer limit;

    protected Integer page;

    protected Pager pager;


    @ModelAttribute
    protected void initRequestResponseSessionHeaders(HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     @RequestHeader HttpHeaders headers,
                                                     RedirectAttributes redirectAttributes) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
        this.headers = headers;

        String sortStr = request.getParameter("sord");
        String sidx = request.getParameter("sidx");
        String limitStr = request.getParameter("limit");
        String offsetStr = request.getParameter("offset");
        String pageStr = request.getParameter("page");

        pager = new Pager();
        if (StringUtils.isNotEmpty(pageStr)) {
            logger.debug("####页码参数page:" + pageStr);
            page = Integer.parseInt(pageStr);
            pager.setPage(page);
            if (StringUtils.isNotEmpty(sortStr)) {
                pager.setSord(sortStr);
            }
            if (StringUtils.isNotEmpty(sidx)) {
                pager.setSidx(sidx);
            }
        }

        if (StringUtils.isNotEmpty(limitStr)) {
            limit = Integer.parseInt(limitStr);
        } else if (limitStr == null || "0".equals(limitStr)) {
            limit = GlobleConstant.DEFAULT_PAGE_SIZE;
        }

        if (StringUtils.isEmpty(offsetStr)) {
            offset = 0;
        } else {
            offset = Integer.parseInt(offsetStr) * limit;
        }
    }

    public boolean hasRoleCode(String roleCode) {
        boolean flag = false;
        String userRoleCodes = (String) session.getAttribute("userRoleCodes");
        String userRoleCodesArr[] = userRoleCodes.split("!");
        for (String userRoleCode : userRoleCodesArr) {
            if (roleCode.equals(userRoleCode)) {
                flag = true;
                break;
            }
        }
        return flag;
    }


    protected JSONResult jsonResult(String code, String msg, Object data) {
        return new JSONResult(code, msg, data);
    }





}
